package cn.fxl.user.controller;

import cn.fxl.basic.exception.BusinessExciption;
import cn.fxl.basic.pageList.GmResult;
import cn.fxl.basic.result.AjaxResult;
import cn.fxl.user.domain.User;
import cn.fxl.user.dto.UserDto;
import cn.fxl.user.query.UserQuery;
import cn.fxl.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(value = "员工的API",description = "员工相关的CRUD功能")
public class UserController {
    @Autowired
    private IUserService iUserService;

    @GetMapping
    @ApiOperation("获取所有员工信息")
    public List<User> getAll() {
        return iUserService.getAll();
    }

    @GetMapping("/{id}")
    @ApiOperation("获取一个员工信息")
    public User getOne(@PathVariable("id") Long id) {
        return iUserService.getOne(id);
    }

    @PostMapping
    @ApiOperation("高级查询分页查询")
    public GmResult<User> querPage(@RequestBody UserQuery userQuery) {
        return iUserService.Pagequery(userQuery);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除员工")
    public AjaxResult del(@PathVariable("id") Long id) {
        try {
            iUserService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(false, "删除失败");
        }
    }

    @PutMapping
    @ApiOperation("新增或修改有id为修改，没用id为新增")
    public AjaxResult saveAndUpdate(@RequestBody User user) {
        try {
            if (user.getId() == null) {
                iUserService.save(user);
            } else {
                iUserService.update(user);
            }
            return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(false, "操作失败");
        }
    }
    @PatchMapping
    public AjaxResult pathchdel(@RequestBody Long[] a){
        try {
            iUserService.pathchdel(a);
            return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(false,"删除失败");
        }
    }
    @PostMapping("/register/email")
    public AjaxResult registeredEmail(@RequestBody User user){
        try {
            iUserService.save(user);
            return new AjaxResult();
        }catch (BusinessExciption businessExciption){
            return new AjaxResult(false,businessExciption.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙");
        }
    }
    @PostMapping("/register/phone")
    public AjaxResult registeredEmail(@RequestBody UserDto userDto){
        try {
            iUserService.phoneRegister(userDto);
            return new AjaxResult();
        }catch (BusinessExciption businessExciption){
            return new AjaxResult(false,businessExciption.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙");
        }
    }
    @GetMapping("/active/email/{id}")
    public void emailActive(@PathVariable("id") Long id){
        iUserService.emailActive(id);
    }
}
